package cn.shop.admin.controller;

import cn.shop.admin.service.HomeService;
import cn.shop.utils.ApiResult;
import cn.shop.utils.constant.Constant;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * 首页接口
 *
 * @author : 李一涵
 * @date : Created in 2022-4-6 14:16
 * @description :
 */
@RestController
@RequestMapping("/api/v1/home")
public class HomeController {

	@Resource
	private HomeService homeService;

	/**
	 * 获取总余额
	 *
	 * @return 返回结果
	 */
	@GetMapping("/balance")
	public ApiResult<BigDecimal> balance() {
		return ApiResult.success(homeService.balance());
	}

	/**
	 * 交易成功统计接口
	 *
	 * @param startTime 开始时间
	 * @param endTime   结束时间
	 * @return 返回结果
	 */
	@GetMapping("/statistics/order")
	public ApiResult<Map<String, Object>> orderStatistics(@RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime startTime,
	                                                      @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime endTime) {
		// if (startTime == null && endTime == null) {
		// 	LocalDateTime now = LocalDateTime.now();
		// 	startTime = now.withHour(0).withMinute(0);
		// 	endTime = now.withHour(23).withMinute(59);
		// }
		return ApiResult.success(homeService.orderStatistics(startTime, endTime));
	}

	/**
	 * 提现成功统计接口
	 *
	 * @param startTime 开始时间
	 * @param endTime   结束时间
	 * @return 返回结果
	 */
	@GetMapping("/statistics/withdraw")
	public ApiResult<Map<String, Object>> withdrawStatistics(@RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime startTime,
	                                                         @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime endTime) {
		// if (startTime == null && endTime == null) {
		// 	LocalDateTime now = LocalDateTime.now();
		// 	startTime = now.withHour(0).withMinute(0);
		// 	endTime = now.withHour(23).withMinute(59);
		// }
		return ApiResult.success(homeService.withdrawStatistics(startTime, endTime));
	}

}
